require('colors')
const fs = require('fs')
const request = require('request-promise')
const cheerio = require('cheerio')
const moment = require('moment')
const md5 = require('../../lib/md5')
const redis = require('../../lib/redis')
const logger = require('../../config/log4j')

const siteName = '拉勾网'
const siteUrl = 'https://www.lagou.com'
const entryUrl = `${siteUrl}/jobs/positionAjax.json?px=new&city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false&isSchoolJob=0`

let globalIndex = []
const idxFilename = `data/${'lagou-' + moment().format('YYYY-MM-DD')}.idx`
const getIndexData = filepath => {
  try {
    const content = fs.readFileSync(filepath, 'utf-8')

    const arr = content.split(',')
    return arr
  } catch (err) {}
  return []
}

const isAppend = rawKey => {
  const key = md5(rawKey).substring(0, 8)
  if (globalIndex.indexOf(key) !== -1) return false
  globalIndex.push(key)
  fs.appendFile(idxFilename, `${key},`, function() {
    console.log('创建索引成功'.green)
  })
  return true
}

const appendFile = item => {
  const filename = 'data/' + moment().format('YYYY-MM-DD') + '.json'
  if (isAppend(item.url)) {
    const itemStr = JSON.stringify(item)
    // 写入文件
    fs.appendFile(filename, itemStr + '\n', function() {
      console.log('消息写入文件成功'.green)
    })
    console.log(itemStr)
    // 写入redis
    redis.queueIn(item)
  }
}

const headers = {
  'accept': 'application/json, text/javascript, */*; q=0.01',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
  'cache-control': 'max-age=0',
  'connection': 'keep-alive',
  'host': 'www.lagou.com',
  'origin': siteUrl,
  'cookie': 'JSESSIONID=ABAAABAABEEAAJA13C1AD5036317A5C7B9C64D22FD7CDD3; user_trace_token=20180301184629-47f04fdc-ea61-4133-8b66-1123b85eb367; _ga=GA1.2.203100916.1519901190; _gid=GA1.2.2048688137.1519901190; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1519901190; LGUID=20180301184629-ccad0fda-1d3d-11e8-97dc-525400f775ce; index_location_city=%E5%8C%97%E4%BA%AC; LGSID=20180301195624-908fded3-1d47-11e8-b108-5254005c3644; PRE_UTM=; PRE_HOST=; PRE_SITE=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist_react%2520%25E5%2589%258D%25E7%25AB%25AF%3Fpx%3Ddefault%26city%3D%25E5%258C%2597%25E4%25BA%25AC; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1519905983; LGRID=20180301200622-f58848e9-1d48-11e8-b108-5254005c3644; SEARCH_ID=c1406daf632e4387b44a7d4481c9aeb3',
  'upgrade-insecure-requests': 1,
  'Referer': 'https://www.lagou.com/jobs/list_react%20%E5%89%8D%E7%AB%AF?px=new&city=%E5%8C%97%E4%BA%AC',
  'x-anit-forge-code': 0,
  'x-anit-forge-token': 'None',
  'x-requested-with': 'XMLHttpRequest',
  'user-agent':
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'
}

async function getPositionResult() {
  try {
    const jsonStr = await request.post({
      url: entryUrl,
      form: {
        first: true,
        pn: 1,
        kd: 'react 前端'
      },
      headers: headers
    })
    const data = JSON.parse(jsonStr)
    return data.content.positionResult.result
  } catch (err) {
    console.log('【拉钩】request请求失败'.red)
    logger.error('【拉钩】request请求失败'.red)
    return []
  }
}

const filterItem = () => item => {
  const today = moment().format('YYYY-MM-DD')
  const reg = new RegExp(`^${today}.*`)
  return reg.test(item.createTime)
}

const parseItem = () => function (item) {
  const type = 'zhaopin'
  const title = `${item.companyShortName} 招聘 ${item.positionName}，${item.salary}`
  const desc = `
  要求: ${item.workYear}/${item.education}<br />
  公司规模: ${item.companySize}/${item.financeStage}<br />
  ${item.positionAdvantage}<br />
  ${item.companyLabelList}
  `
  const price = item.salary
  const url = `${siteUrl}/jobs/${item.positionId}.html`
  const company = item.companyShortName
  const source = siteName
  const date = new Date()
  const data = { type, title, desc, price, url, company, source, date }
  appendFile(data)
}

async function updateData() {
  const data = await getPositionResult()
  data.filter(filterItem()).forEach(parseItem())
}

async function lagouSpider() {
  globalIndex = getIndexData(idxFilename)
  await updateData()
}

module.exports = lagouSpider